//
//  TPRootViewController.m
//  CloudAtCostPanel
//
//  Created by leonardo on 2017/1/26.
//  Copyright © 2017年 tanpengsccd. All rights reserved.
//

#import "TPRootViewController.h"

#import "TPLoginViewController.h"
#import "TPTabBarController.h"
#import "TPBaseNavigationViewController.h"
@interface TPRootViewController ()
//PRO_S TPBaseNavigationViewController * loginVC;
PRO_S TPTabBarController * mainVC;
@end

@implementation TPRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    //加上欢迎界面和登录界面的通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoIdentityVC) name:@"WelcomeDidFinish" object:nil];
    //加上重新登录的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoLoginVC) name:NOTIFICATION_GO_LOGIN object:nil];
    //加上登录结束的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoMainVC) name:NOTIFICATION_GO_MAIN object:nil];
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginDidFinish" object:nil];
    
    _mainVC = [TPTabBarController new];
    
    
   
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSString * login = [[NSUserDefaults standardUserDefaults] objectForKey:KEY];
    if (!login) {
        [self gotoLoginVC];
    }else{
        [self gotoMainVC];
        
    }
}
- (void)gotoLoginVC{
    
    TPBaseViewController * existingVC =  [[self childViewControllers] firstObject];
    
    TPLoginViewController * loginVC = [TPLoginViewController new];
   TPBaseNavigationViewController * loginNavVC = [[TPBaseNavigationViewController alloc]initWithRootViewController:loginVC];
    [self addChildViewController:loginNavVC];
    [self.view addSubview:loginNavVC.view];
    
    if (existingVC) {
        [self transitionFromViewController:existingVC toViewController:loginNavVC duration:1 options:UIViewAnimationOptionTransitionNone | UIViewAnimationOptionCurveEaseInOut animations:^{
            loginNavVC.view.alpha = 1;
            existingVC.view.alpha = 0;
        } completion:^(BOOL finished) {
            [existingVC.view removeFromSuperview];
            [existingVC removeFromParentViewController];
        }];
    }else{
        
    }
}

- (void)gotoMainVC{
   TPBaseViewController * existingVC =  [[self childViewControllers] firstObject];
    
    [self addChildViewController:_mainVC];
    [self.view addSubview:_mainVC.view];
    
    if (existingVC) {
        [self transitionFromViewController:existingVC toViewController:_mainVC duration:1 options:UIViewAnimationOptionTransitionNone | UIViewAnimationOptionCurveEaseInOut animations:^{
            _mainVC.view.alpha = 1;
            existingVC.view.alpha = 0;
        } completion:^(BOOL finished) {
            [existingVC.view removeFromSuperview];
            [existingVC removeFromParentViewController];
        }];
    }else{
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
